/* eslint-disable max-lines */
import TauSimpleSelect from '@/components/TauSimpleSelect';
import * as BASE from '@/stores/definitions-base';
import { DictItemSelect } from '@/components/biz';
import { DatePicker, Switch } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

/**
 * @param {*} path        属性path
 * @param {*} props       column属性
 * @returns column定义
 */
export function column(...args) {
  return columnBase(companyModal, ...args);
}

/**
 * 显示一组表单字段
 * ```js
 * {
 *   fields: string[],    // 字段名称，可以使用.来指定嵌套对象属性，例如order.busOrder.shift
 *   columns: number,     // 列数，默认为4,最大为12,最小为1
 *   model: object,       // 字段定义数据模型的根对象，默认不指定，仅在使用子对象作为根模型时设置
 *   overrideModel: object, // 重载字段定义数据模型
 *   mode: string,        // 表单模式，默认为edit，可选view
 *   itemLayout: object,  // FormItem布局配置，默认为{}
 * }
 * ```
 *
 * @param {object} param0
 * @returns
 */
export function FieldsGrid(props) {
  return <FieldGridBase model={companyModal} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  // ：1-包车订单 2-班车订单 3-巴士订单
  orderType: {
    1: t`包车订单`,
    2: t`专线订单`,
    3: t`巴士订单`,
  },
  // <br/> 必填<br/> - 1 启用（默认）<br/> - 0 禁用 禁用时，不派单给该公司旗下车辆
  statusName: {
    1: t`启用`,
    0: t`禁用`,
  },
  status: {
    1: true,
    0: false,
  },
  // <br/> - 香港HK<br/> - 大陆CN<br/> - 澳门OM
  cityName: {
    HK: t`香港`,
    CN: t`大陆`,
    OM: t`澳门`,
  },
  period: {
    0: 'D+0',
    1: 'D+1',
    2: 'D+2',
    7: 'D+7',
    30: 'D+30',
  },
  // <br/> MERCHANT_ID：商户号<br/> PERSONAL_OPENID：个人openid
  type: {
    MERCHANT_ID: '商户号',
    PERSONAL_OPENID: '个人',
  },
};

/**
 * 模型定义:
 * 1. 普通文本仅需要定义label
 * 2. 需要通过枚举转换的值，定义text; 当使用renderer返回组件时，text属性无效，需要通过组件的renderPreview来指定预览时的绘制方式
 * 3. 通用的ui属性需要定制时，定义ui; 特定模式(view, edit, add)下的ui，添加后缀定义为ui_<mode>，例如：ui_view, ui_edit, ui_add, ui_table
 * 4. ui可配置属性包括:
 *    renderer: 内容渲染函数, Form表单中的原型为：(value: any, formData: any, fieldDefinition: any) => ReactNode; Table Column中的原型为: (value: any, columnIndex: number, rowData: any) => ReactNode;
 *    itemProps: 表单Form.Item的属性，可以是对象，或函数({ isPreview: boolean, mode: string, value: any, formData: any, ...otherDefinitions}) => void;
 *    span: Grid Cell占用的列数, 默认为1
 *
 * 【注】获取接口的model定义后，vscode中通过正则替换：^(\s+)"label": "([^']+)"  ===> $1label: t`$2`
 */
export const companyModal = {
  mchId: {
    label: t`二级商户名称`,
    ui: {
      renderer: (v) => <TauSimpleSelect data={rootStore.companyStore.payMerchant} value={v} />,
      itemProps: { required: true },
    },
  },
  directorIdTypeName: {
    label: t`证件名称`,
  },
  bandBankRMB_Name: {
    label: t`RMB银行名称`,
  },
  bandBankHKD_Name: {
    label: t`HKD银行名称`,
  },
  bandBankMOP_Name: {
    label: t`MOP银行名称`,
  },
  vehicleNum: {
    label: t`车辆统计`,
  },
  driverNum: {
    label: t`司机统计`,
  },
  ccOrderNum: {
    label: t`包车订单`,
  },
  scOrderNum: {
    label: t`专线订单`,
  },
  code: {
    label: t`公司id<br/> 2位注册地区编码+4位数字<br/> - 香港HK<br/> - 大陆CN<br/> - 澳门OM`,
  },
  name: {
    label: t`名称`,
    ui: {
      itemProps: { required: true },
    },
  },
  registerAddr: {
    label: t`注册地区`,
    text: (v) => ENUMS.cityName[v],
    ui: {
      renderer: (v) => <TauSimpleSelect data={toSelectDataSource(ENUMS.cityName)} value={v} />,
      itemProps: { required: true },
    },
  },
  tel: {
    label: t`公司电话`,
    ui: {
      itemProps: { required: true },
    },
  },
  address: {
    label: t`联系地址`,
    ui: {
      itemProps: { required: true },
    },
  },
  singleDriver: {
    label: t`个人公司`,
    ui: {
      renderer: (v) => <Switch checkedChildren="是" unCheckedChildren="否" autoWidth size="small" checked={v} />,
    },
  },
  independentAccount: {
    label: t`分账设置信息`,
    // ：T+0、T+1、T+2、T+7、T+30(加几值为几，例：T+0 值为0)
    period: {
      label: t`分账周期`,
      ui: {
        renderer: (v) => <TauSimpleSelect data={toSelectDataSource(ENUMS.period)} value={v} />,
        itemProps: { required: true },
      },
    },
    //  true：暂停 false：可分账
    suspensionOfDistribution: {
      label: t`暂停分账`,
      ui: {
        renderer: (v) => <Switch size="small" checked={v} />,
      },
    },
    settlementOfDriverCharges: {
      label: t`是否委托平台自动结算司机费用`,
      ui: {
        renderer: (v) => <Switch size="small" checked={v} />,
      },
    },
    collectionAccountInformation: {
      label: t`收款账户信息`,
      appId: {
        label: t`公众号appId`,
      },
      accountIdentification: {
        label: t`账户标识`,
      },
      name: {
        label: t`名字`,
      },
      // <br/> MERCHANT_ID：商户号<br/> PERSONAL_OPENID：个人openid
      type: {
        label: t`分账接收方类型`,
        ui: {
          renderer: (v) => <TauSimpleSelect data={toSelectDataSource(ENUMS.type)} value={v} />,
          itemProps: { required: true },
        },
      },
    },
  },
  orgCode: {
    label: t`工商编码`,
    ui: {
      itemProps: { required: true },
    },
  },
  // <br/> - false 自动<br/> - true 手工
  isManual: {
    label: t`手工派单`,
    ui: {
      renderer: (v) => <Switch checkedChildren="是" unCheckedChildren="否" autoWidth size="small" checked={v} />,
    },
  },
  // <br/> 必填，法人代表等
  orgDirector: {
    label: t`企业负责人`,
    ui: {
      itemProps: { required: true },
    },
  },
  directorIdType: {
    label: t`负责人证件类型`,
    ui: {
      renderer: (v) => <DictItemSelect dictId="directorIdType" value={v} />,
    },
  },
  directorId: {
    label: t`负责人证件号`,
  },
  directorTel: {
    label: t`负责人电话`,
    ui: {
      itemProps: { required: true },
    },
  },
  dateOfIncorporate: {
    label: t`成立日期`,
    ui: {
      renderer: (v) => <DatePicker style={{ width: '100%' }} type="datetime" value={DATETIME_TEXT(v)} />,
      itemProps: { required: true },
    },
  },
  // <br/> 必填<br/> - 1 启用（默认）<br/> - 0 禁用 禁用时，不派单给该公司旗下车辆
  status: {
    label: t`公司状态`,
    text: (v) => ENUMS.statusName[v],
    ui_table: {
      renderer: (_, __, r) => <Switch checkedChildren="启用" unCheckedChildren="禁用" autoWidth size="small" checked={ENUMS.status[r.status]} onClick={() => rootStore.companyStore.updateStatus(r)} />,
    },
    ui: {
      renderer: (v) => <Switch checkedChildren="启用" unCheckedChildren="禁用" autoWidth size="small" checked={v} defaultChecked={false} />,
    },
  },
  bankAccounts: {
    label: t`开户行信息`,
    '{key}': {
      bank: {
        label: t`[key]开户银行`,
        ui: {
          renderer: (v) => <DictItemSelect dictId="bank" value={v} renderPreview={(itm) => EMPTY_TEXT(itm?.name)} />,
        },
      },
      accountName: {
        label: t`[key]账户名称`,
      },
      account: {
        label: t`[key]银行账号`,
        ui: {
          span: 2,
        },
      },
    },
  },
  hasSurcharge: {
    label: t`是否设置运营附加费<br/> - true 已设置，<br/> - false 未设置`,
  },
  attachments: [{
    label: t`附件`,
    title: {
      label: t`附件标题`,
    },
    description: {
      label: t`附件说明`,
    },
    binaryId: [{
      label: t`附件id`,
    }],
  }],
  crBy: {
    label: t`创建者`,
  },
  upBy: {
    label: t`最后修改者`,
  },
  crDate: {
    label: t`创建时间`,
  },
  upDate: {
    label: t`更新时间`,
    text: DATETIME_TEXT,
  },
  tenantId: {
    label: t`租户ID`,
  },
  id: {
    label: 'ID',
  },
  operating: {
    label: '专线线路运营公司',
    ui: {
      renderer: (v) => <Switch size="small" checkedChildren="是" unCheckedChildren="否" autoWidth checked={v} />,
    },
  },
  orderTime: {
    label: t`禁止接单时间`,
  },
  stopOrder: {
    label: t`禁止接单`,
    ui: {
      renderer: (v) => <Switch size="small" checkedChildren="是" unCheckedChildren="否" autoWidth checked={v} />,
    },
  },
  // 停止接单信息
  stopTakingOrders: {
    label: t`停止接单信息`,
    stopOrder: {
      label: t`停止接单`,
      ui: {
        renderer: (v) => <Switch size="small" checkedChildren="是" unCheckedChildren="否" autoWidth checked={v} />,
      },
    },
    stopFrom: {
      label: t`起始停止接单时间`,
    },
    stopTo: {
      label: t`结束停止接单时间`,
    },
    // 原线路权重
    oldPriceWeight: {
      label: t`原线路权重`,
    },
  },
  // 前端新增
  logo: {
    label: t`公司logo`,
  },
};
